<template>
	<!-- 我的寄售 -->
	<!-- 顶部查询 -->
	<view class="content">
		<el-form ref="form" :model="form" label-width="80px" class="orderStatus">
			<el-form-item label="商品名称:" class="order">
				<el-input v-model="form.Article_Name" style="width: 188px" placeholder="请输入商品名称"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="handleclick()">发布</el-button>
				<el-button type="success">查询</el-button>
				<el-button type="danger" @click="Black">返回</el-button>
			</el-form-item>
		</el-form>
		<el-table :data="tableData" border :header-cell-style="{ 'text-align': 'center' }"
			:cell-style="{ 'text-align': 'center' }" style="width: 97%;" height="calc(100vh - 140px)">
			<el-table-column type="index" label="序号" width="50"></el-table-column>
			<el-table-column prop="Picture_URL" label="商品图片" width="85" align="center">
				<template v-slot="{row}">
					<image style="width: 120rpx;height: 120rpx;" :src="Domain_IMG+row.Picture_URL" mode=""></image>
				</template>
			</el-table-column>
			<el-table-column prop="Article_Name" label="商品名称" width="85" align="center">
				<template v-slot="{row}">
					<el-tooltip effect="dark" :content="row.Article_Name" placement="top">
						<view class="text-ellipsis w-100">
							<text>{{row.Article_Name}}</text>
						</view>
					 </el-tooltip>
				</template>
			</el-table-column>
			<el-table-column prop="Quantity" label="购买数量">
				<template v-slot="{row}">
					{{row.Quantity}}{{row.Unit}}
				</template>
			</el-table-column>
			<el-table-column prop="Amount" label="应付金额" width="85">
				<template v-slot="{row}">
					￥{{Number(row.Amount).toFixed(2)}}
				</template>
			</el-table-column>
			<el-table-column prop="Collection_Amount" label="已付金额">
				<template v-slot="{row}">
					￥{{Number(row.Collection_Amount).toFixed(2)}}
				</template>
			</el-table-column>
			<el-table-column prop="Amount" label="未付金额" width="85">
				<template v-slot="{row}">
					￥{{(Number(row.Amount)-Number(row.Collection_Amount)).toFixed(2)}}
				</template>
			</el-table-column>
			<el-table-column prop="Trading_Name" label="付款方式" width="85">
				<template v-slot="{row}">
					<el-tooltip effect="dark" :content="row.Trading_Name" placement="top">
						<view class="text-ellipsis w-100">
							<text>{{row.Trading_Name}}</text>
						</view>
					 </el-tooltip>
				</template>
			</el-table-column>
			<el-table-column prop="Buyer_Contact" label="联系人" width="70">
				<template v-slot="{row}">
					<el-tooltip effect="dark" :content="row.Buyer_Contact" placement="top">
						<view class="text-ellipsis w-100">
							<text>{{row.Buyer_Contact}}</text>
						</view>
					 </el-tooltip>
				</template>
			</el-table-column>
			<el-table-column prop="Row_Status" label="状态" width="120">
				<template v-slot="{row}">
					<view class="text-ellipsis w-100">
						<text v-if="row.Row_Status == '0' && row.Buyer_Status == '0'">待审核</text>
								<text v-if="row.Row_Status == '2' && row.Buyer_Status == '0'">已拒绝</text>
								<text v-if="(row.Row_Status == '1' && row.Buyer_Status == '0') || (row.Row_Status == '1' && row.Buyer_Status == '4')">待确认合同</text>
								<text v-if="row.Row_Status == '4' && row.Buyer_Status == '0'">待对方确认合同</text>
								<text v-if="row.Row_Status == '5'">待收货</text>
								<view v-if="row.Trading_Name != '全款'" class="">
									<text v-if="ow.Row_Status == '4' &&row.Buyer_Status == '4' &&row.Trading_Detail_SerialNumber.includes('T01000000000001_1')">待发货</text>
									<text v-if="
						    row.Row_Status == '4' &&
						    row.Trading_Detail_SerialNumber.includes('T01000000000001_0') &&
						    row.Buyer_Status != '0'
						  ">待对方付首款</text>
									<text v-if="
						    row.Row_Status == '6' &&
						    row.Trading_Detail_SerialNumber.includes('T01000000000002_0') &&
						    row.Trading_Detail_SerialNumber.includes('T01000000000001_1')
						  ">待对方付下一笔款</text>
									<text v-if="
						    row.Row_Status == '6' &&
						    row.Trading_Detail_SerialNumber.includes('T01000000000003_0') &&
						    row.Trading_Detail_SerialNumber.includes('T01000000000002_1')
						  ">待对方付下一笔款</text>
									<text v-if="
						    row.Row_Status == '6' &&
						    row.Trading_Detail_SerialNumber.includes('T01000000000004_0') &&
						    row.Trading_Detail_SerialNumber.includes('T01000000000003_1')
						  ">待对方付尾款</text>
								</view>
								<view v-if="row.Trading_Name == '全款'" class="">
									<text v-if="
						    row.Row_Status == '4' &&
						    row.Buyer_Status == '4' &&
						    row.Trading_Detail_SerialNumber.includes('T01000000000001_0')
						  ">待对方付全款</text>
									<text v-if="
						    row.Row_Status == '4' &&
						    row.Buyer_Status == '4' &&
						    row.Trading_Detail_SerialNumber.includes('T01000000000002_0')
						  ">待对方付全款</text>
									<text v-if="
						    row.Row_Status == '4' &&
						    row.Buyer_Status == '4' &&
						    row.Trading_Detail_SerialNumber.includes('T01000000000003_0')
						  ">待对方付全款</text>
									<text v-if="
						    row.Row_Status == '4' &&
						    row.Buyer_Status == '4' &&
						    row.Trading_Detail_SerialNumber.includes('T01000000000004_0')
						  ">待对方付全款</text>
									<text v-if="
						    row.Row_Status == '4' &&
						    row.Buyer_Status == '4' &&
						    row.Trading_Detail_SerialNumber.includes('T01000000000001_1')
						  ">待发货</text>
									<text v-if="
						    row.Row_Status == '4' &&
						    row.Buyer_Status == '4' &&
						    row.Trading_Detail_SerialNumber.includes('T01000000000002_1')
						  ">待发货</text>
									<text v-if="
						    row.Row_Status == '4' &&
						    row.Buyer_Status == '4' &&
						    row.Trading_Detail_SerialNumber.includes('T01000000000003_1')
						  ">待发货</text>
									<text v-if="
						    row.Row_Status == '4' &&
						    row.Buyer_Status == '4' &&
						    row.Trading_Detail_SerialNumber.includes('T01000000000004_1')
						  ">待发货</text>
								</view>
								<text v-if="row.Row_Status == '6' && row.Buyer_Status == '4' &&Number(row.Amount) == Number(row.Collection_Amount)">交易结束</text>
					</view>
				</template>
			</el-table-column>
			<el-table-column :show-overflow-tooltip="true" effect="dark" prop="Enterprise_Name" label="买方企业"
				width="150"></el-table-column>
			<el-table-column align="center" label="操作" width="150">
				<template v-slot="{row}">
					<view class="flex justify-center align-center">
						<el-button :loading="loading" @click="agree(row)" v-if="row.Row_Status == '0'" size="mini"
							type="primary">通过</el-button>
						<el-button :loading="loading" @click="refuse(row)" v-if="row.Row_Status == '0'" size="mini"
							type="danger">拒绝</el-button>
						<el-button :loading="loading" @click="Confirmcontract(row)" v-if="row.Row_Status == '1'" size="mini"
							type="primary">确认合同</el-button>
						<el-button :loading="loading" @click="toggle(row)"
							v-if="row.Row_Status == '4'&&row.Buyer_Status == '4'&&row.Paystages.split('/')[0]=='1'"
							size="mini" type="success">发货</el-button>
					</view>
				</template>
			</el-table-column>
		</el-table>
		<el-dialog title="物流信息" :visible.sync="TrackingNumber" width="30%" center>
			<el-input placeholder="请输入物流信息" v-model="trackingnumber" clearable></el-input>
			<view slot="footer" class="dialog-footer">
				<el-button @click="TrackingNumber = false">取 消</el-button>
				<el-button :loading="loading" type="primary" @click="Consignment()">发货</el-button>
			</view>
		</el-dialog>
		<view class="flex " style="justify-content: space-between;">
			<view class="">
				
			</view>
			<el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="form.page" :page-sizes="[1,5,10,20]" :page-size="form.limit"
				layout="total, sizes, prev, pager, next, jumper" :total="total">
			</el-pagination>
		</view>
	</view>

</template>

<script>
	const app = getApp()
	import {
		Message
	} from 'element-ui'
	import times from '@/static/js/utils.js'
	import {
		getGroupTransactionAllAction
	} from '@/api/purchasedgoods.js'
	import {
		GroupCheckStockAction,
		GroupTransactionExamineAction,
		GroupProcureAllAction,
		GroupProcureAll
	} from '@/api/group.js'
	import {
		getSubtableInquiryOriginal,
		getEditAction
	} from '@/api/global.js'
	export default {
		name: "mySupply",
		data() {
			return {
				loading:false,
				Domain_IMG: app.globalData.Domain_IMG,
				form: {
					Type: "1",
					Group_Purchasing_SerialNumber: '',
					Enterprise_Info_SerialNumber: uni.getStorageSync('loginInfo').enterprise,
					page: 1, //页数
					limit: 5, //每页数据条数
				},
				total: 0,
				tableData: [],
				tableData2:[],
				TrackingNumber: false,
				trackingnumber:'',
				Group_Transaction_SerialNumber:'',
				group_transaction_logistics: {
					Group_Logistics_SerialNumber: '', //主键
					Group_Transaction_SerialNumber: '', //交易流水号
					Transport_Type: '', //运输方式
					Transport_Explain: '' //说明
				},
			};
		},
		mounted() {
			this.getList(this.form);
		},
		methods: {
			handleclick() {
				uni.navigateTo({
					url: '/pages/release/supply-release'
				})
			},
			Black() {
				this.$emit('Black',7)
			},
			handleCurrentChange(e) {
				this.form.page = e
				this.getList(this.form)
			},
			handleSizeChange(e) {
				this.form.page = 1
				this.form.limit = e
				this.getList(this.form)
			},
			async getList(data) {
				//分页查询所有采购数据
				const res = await getGroupTransactionAllAction({
					data,
				})
				console.log(res);
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.navigateTo({
						url:'/pages/login/login?expire=1'
					})
				} else if (res.Status == 'success') {
					this.tableData = res.tableData
					this.total = parseInt(res.limit)
					console.log(res.tableData);
				}
			},
			async agree(row) {
				console.log(row)
				this.loading = true
				let data = {
					Group_Purchasing_SerialNumber: row.Group_Purchasing_SerialNumber,
					Quantity: row.Quantity,
					token: uni.getStorageSync('loginInfo').token,
				}
				const res = await GroupCheckStockAction({
					data,
				})
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.navigateTo({
						url:'/pages/login/login?expire=1'
					})
				} else if (res.Status == 'success') {
					console.log(res);
					let Data = {
						Group_Transaction_SerialNumber: row.Group_Transaction_SerialNumber,
						Audit_Status: '1',
						Cause: '',
						token: uni.getStorageSync('loginInfo').token,
					}
					this.ExamineAction(Data)
				}
			},
			refuse(row) {
				this.loading = true
				let Data = {
					Group_Transaction_SerialNumber: row.Group_Transaction_SerialNumber,
					Audit_Status: '2',
					Cause: '拒绝',
					token: uni.getStorageSync('loginInfo').token,
				}
				this.ExamineAction(Data)
			},
			async ExamineAction(data) {
				//分页查询所有采购数据
				const res = await GroupTransactionExamineAction({
					data,
				})
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.navigateTo({
						url:'/pages/login/login?expire=1'
					})
				} else if (res.Status == 'success') {
					console.log(res);
					if (data.Audit_Status == '1') {
						Message({
							message: '已通过',
							type: 'success'
						})
					} else if (data.Audit_Status == '2') {
						Message({
							message: '已拒绝',
							type: 'success'
						})
					}
					this.loading = false
					this.getList(this.form)
				}
			},
			async getOriginal(data) {
				//分页查询所有采购数据
				const res = await getSubtableInquiryOriginal({
					data,
				})
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.navigateTo({
						url:'/pages/login/login?expire=1'
					})
				} else if (res.Status == 'success') {
					console.log(res);
					let group_message_buyer = {
						Group_Message_SerialNumber: "",
						Group_Transaction_SerialNumber: res.group_transaction[0].Group_Transaction_SerialNumber,
						Cause: "确认合同",
						Submission_Time: times.nowtime(),
						Enterprise_Info_SerialNumber: uni.getStorageSync('loginInfo').enterprise,
						Seller_Type: "0"
					}
					res.group_transaction[0].Row_Status = '4'
					let Data = {
						Data: JSON.stringify({
							Modify: {
								group_transaction: [res.group_transaction[0]]
							},
							Add: {
								group_message: [group_message_buyer]
							}
						}),
						token: uni.getStorageSync('loginInfo').token,
					}
					this.getEdit(Data)
				}
			},
			async shipments(data) {
				const res = await getSubtableInquiryOriginal({
					data,
				})
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.navigateTo({
						url:'/pages/login/login?expire=1'
					})
				} else if (res.Status == 'success') {
					console.log(res);
					let group_message_buyer = {
						Group_Message_SerialNumber: "",
						Group_Transaction_SerialNumber: this.Group_Transaction_SerialNumber,
						Cause: "已发货",
						Submission_Time: times.nowtime(),
						Enterprise_Info_SerialNumber: uni.getStorageSync('loginInfo').enterprise,
						Seller_Type: "0"
					}
					res.group_transaction[0].Row_Status = '5'
					let Data = {
						Data: JSON.stringify({
							Modify: {
								group_transaction: [res.group_transaction[0]]
							},
							Add: {
								group_transaction_logistics: [this.group_transaction_logistics],
								group_message: [group_message_buyer]
							},
						}),
						token:uni.getStorageSync('loginInfo').token,
					}
					this.getEdit(Data)
				}
			},
			async getEdit(data) {
				//分页查询所有采购数据
				const res = await getEditAction({
					data,
				})
				if (res.redirect && res.redirect == 'REDIRECT') {
					uni.navigateTo({
						url:'/pages/login/login?expire=1'
					})
				} else if (res.Status == 'success') {
					console.log(res);
					if(this.trackingnumber.trim()!=''){
						Message({
							message: '已发货',
							type: 'success'
						})
						this.TrackingNumber = false
						this.getList(this.form)
					}else{
						Message({
							message: '已确认合同',
							type: 'success'
						})
					}
					this.loading = false
					this.getList(this.form)
				}
			},
			Confirmcontract(row) {
				this.loading = true
				let obj = {
					// 采购信息表
					group_transaction: {
						group_transaction_paystages: {}
					}
				}
				let data = {
					Primary_Key: row.Group_Transaction_SerialNumber,
					JSONObject: JSON.stringify(obj),
					token: uni.getStorageSync('loginInfo').token,
				}
				this.getOriginal(data)
			},
			toggle(row) {
				this.TrackingNumber = true
				this.Group_Transaction_SerialNumber = row.Group_Transaction_SerialNumber
				this.group_transaction_logistics.Transport_Type = row.Transport_Name
				this.group_transaction_logistics.Group_Transaction_SerialNumber = row.Group_Transaction_SerialNumber
			},
			Consignment(){
				if(this.trackingnumber.trim()==''){
					Message({
						message: '请先输入物流信息',
						type: 'error'
					})
					return
				}
				this.loading = true
				this.group_transaction_logistics.Transport_Explain = this.trackingnumber
				let obj = {
					// 采购信息表
					group_transaction: {
						group_transaction_paystages: {}
					}
				}
				let data = {
					Primary_Key: this.Group_Transaction_SerialNumber,
					JSONObject: JSON.stringify(obj),
					token: uni.getStorageSync('loginInfo').token,
				}
				this.shipments(data)
			}
		}
	};
</script>

<style>
	.content {
		width: 100%;
		padding: 30rpx;

		background-color: #fff;

		.el-form {
			display: flex;
			flex-wrap: wrap;
		}
	}
</style>